package startup

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"jk-time/webook/internal/dao"
)

func InitDB() *gorm.DB {
	sqlConfig := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc"+
		"=Local", "root", "85614455", "192.168.71.100:3306", "webook")
	db, err := gorm.Open(mysql.Open(sqlConfig))
	if err != nil {
		panic(err)
	}

	//监听变化
	//dao.NewGormUserDAO(func() *gorm.DB {
	//	viper.OnConfigChange(func(in fsnotify.Event) {
	//		db, err = gorm.Open(mysql.Open(sqlConfig))
	//		pt := unsafe.Pointer(&db)
	//		atomic.StorePointer(&pt,unsafe.Pointer(&db))
	//	})
	//	return db
	//})

	err = dao.InitTables(db)
	if err != nil {
		panic(err)
	}
	return db
}
